<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;

class PersonController extends Controller
{
	
	// 我的关注 瀑布流
	public function myFollow( Request $request )
	{
		if( empty(Session::get('user_id')) )
		{
			return redirect('/');
		}
		// 包含 页数 
		$data = $request -> except('_token');
		$page = intval( $data['page'] ) -1 ;
		$per = 5;
		$start = $per * $page;
		
		$myFollows = DB::table('fans as f')
			-> select('f.follow_id','u.name',DB::raw("replace( u.pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url "),'u.desc' )
			-> leftJoin('user as u','u.id','=','f.follow_id')
			-> where( 'f.uid',Session::get('user_id') )
			-> orderBy('f.follow_id','desc')
			// 从 哪开始
			-> offset( $start )
			// 只取10条
			-> limit(8)
			-> get();
		
		if( count($myFollows) < 1 )
		{
			$this -> show_msg('已全部加载');
		}
		
		$all_follow = array();
		foreach( $myFollows as $key=> $follows )
		{
			
			$follow = array();
			$follow['name'] = $follows->name;
			$follow['pic_url']  = $follows->pic_url;
			$follow['follow_id']  	= $follows->follow_id;
			$follow['desc']  	= $follows->desc;
			$all_follow[$key]   = $follow;
		
		}
		
		// 传递 数组 json 格式
		exit( json_encode($all_follow) );
	}
	
	
	// 我的粉丝 瀑布流
	public function myFans( Request $request )
	{
		if( empty(Session::get('user_id')) )
		{
			return redirect('/');
		}
		
		// 包含 页数 
		$data = $request -> except('_token');
		$page = intval( $data['page'] ) -1 ;
		$per = 5;
		$start = $per * $page;
		
		$myFans = DB::table('fans as f')
			-> select('u.name as username',DB::raw("replace( u.pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url "),'u.id as uid','u.desc' )
			-> leftJoin( 'user as u','u.id','=','f.uid' )
			// 关注我的
			-> where( 'f.follow_id',Session::get('user_id') )
			-> orderBy('f.uid','desc')
			// 从 哪开始
			-> offset($start)
			// 只取10条
			-> limit(8)
			-> get();
		if( count($myFans) < 1 )
		{
			$this -> show_msg('已全部加载');
		}
		$all_fans = array();
		foreach( $myFans as $key=> $fans )
		{
			
			$follow = array();
			$follow['username'] = $fans->username;
			$follow['pic_url']  = $fans->pic_url;
			$follow['uid']  	= $fans->uid;
			$follow['desc']  	= $fans->desc;
			$all_fans[$key]   = $follow;
		
		}
		
		// 传递 数组 json 格式
		exit( json_encode($all_fans) );
		
	}
	
	
	// 粉丝管理
	public function fansIndex()
	{
		
		if( empty(Session::get('user_id')) )
		{
			return redirect('/');
		}
		
		// 我的关注
		$myFollow = DB::table('fans as f')
			-> select('f.follow_id','f.status','u.name',DB::raw("replace( u.pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url "),'u.desc' )
			-> leftJoin('user as u','u.id','=','f.follow_id')
			-> where( 'f.uid',Session::get('user_id') )
			-> orderBy('f.follow_id','desc')
			// 从 哪开始
			-> offset(0)
			// 只取10条
			-> limit(8)
			-> get();
		
		// 我的粉丝
        DB::connection()->enableQueryLog();
		$myFans = DB::table('fans as f')
			-> select('u.name as username','f.status',DB::raw("replace( u.pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url "),'u.id as uid','u.desc' )
			-> leftJoin( 'user as u','u.id','=','f.uid' )
			// 关注我的
			-> where( 'f.follow_id',Session::get('user_id') )
            //条件
			-> orderBy('f.uid','desc')
			// 从 哪开始
			-> offset(0)
			// 只取5条
			-> limit(8)
			-> get();
			
		// 粉丝总数
		// $fansTotal = DB::table('fans')
				// -> where( 'follow_id',Session::get('user_id') )
				// -> count();
		
		// 全部 关注	
		$follows = DB::table('fans')
			-> select('follow_id')
			-> where( 'uid',Session::get('user_id'))
			-> get();
		$follow_arr = array();
		foreach( $follows as $key=> $follow_id )
		{
			$follow_arr[$key] = $follow_id -> follow_id;
		}
        // $log = DB::getQueryLog();
		// print_r($log);die;
		
		return view('center.focus',['myFollow'=>$myFollow,'myFans'=>$myFans,'follow_arr'=>$follow_arr]);
	
	}

	
	// 个人主页 展示页
	public function index( $user_id='' )
	{
		
		if( empty($user_id) )
		{
			return redirect('/');
		}
		
		// 1. 用户信息
		$user_info = DB::table('user')
					-> select( 'id','name',DB::raw("replace(pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url"),'score','desc' )
					-> where( 'id',$user_id )
					-> first();
		if( empty($user_info) )
		{
			return redirect('/');
		}
					
		// 2. 总发布
		$total = DB::table('new_recommend')
			-> whereIn('status',array(3,4,5))
			-> where('user_id',$user_id)
			-> count();
		// 3. 中的
		$zhong  = DB::table('new_recommend')
				// 中
				-> where('judge',2)
				-> where('user_id',$user_id)
				-> count();
		$user = array();
		if( $total<1 )
		{
			$user['shenglv'] = '0';
		}else
		{
			$user['shenglv'] = ( round($zhong/$total,2)*100 )."%" ;
		}
		
		
		$user['total'] = $total;
		$user['zhong'] = $zhong;
		$user['id'] = $user_info->id;
		$user['name'] = $user_info->name;
		$user['pic_url'] = $user_info->pic_url;
		$user['score'] = $user_info->score;
		$user['desc'] = $user_info->desc;
		
		// 4. 历史竞彩推荐
		$history = DB::table('new_recommend')
			-> select( '*',DB::raw("replace(home_pic,'/images','".config('app.imgPath')."/images' ) as home_pic "),DB::raw("replace(away_pic,'/images','".config('app.imgPath')."/images' ) as away_pic ") )
			-> whereIn('status',array(3,4,5))
			-> where('user_id',$user_id)
			-> orderBy('create_at','desc')
			// 分页
			-> paginate(10);
			
			
		// 5.胜率排行榜
		$all_user = DB::table('new_recommend')
				-> select('user_id')
				-> groupBy('user_id')
				-> get();
		$rank = array();
		foreach( $all_user as $key => $userid )
		{
			// 1.总发布
			$rank_total = DB::table('new_recommend')
				-> whereIn('status',array(3,4,5))
				-> where('user_id',$userid->user_id)
				-> count();
			// 2.中的
			$rank_zhong  = DB::table('new_recommend')
					// 中
					-> where('judge',2)
					-> where('user_id',$userid->user_id)
					-> count();
			// 用户信息
			$user_info = DB::table('user')
						-> select( 'id','name',DB::raw("replace(pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url") )
						-> where( 'id',$userid->user_id )
						-> first();
			if( $rank_total<1 )
			{
				$rank[$key]['shenglv'] = '0';
			}else
			{
				// 2位小数
				$rank[$key]['shenglv'] = ( round($rank_zhong/$rank_total,2)*100)."%";
			}
			$rank[$key]['rank_total'] = $rank_total;
			$rank[$key]['rank_zhong'] = $rank_zhong;
			$rank[$key]['id'] = $user_info->id;
			$rank[$key]['name'] = $user_info->name;
			$rank[$key]['pic_url'] = $user_info->pic_url;
		}
		
		// 按大小排序
		arsort($rank);
		
		
		// 6.球吧文章
		$articles = DB::table('article as a')
			-> select("a.*",'c.name as category_name')
			-> leftJoin('category as c','c.id','=','a.category_id')
			-> where('a.user_id',$user_id)
			-> whereIn( 'a.status',array(3,4,5) )
			-> orderBy('a.create_at','desc')
			// 从 哪开始
			-> offset(0)
			// 只取10条
			-> limit(5)
			-> get();
		$all_wenzhang = array();
		foreach( $articles as $key=> $article_info )
		{
			$wenzhang = array();
			// 文章简短介绍
			$pattern = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
			preg_match_all($pattern,$article_info->content,$arr);
			foreach( $arr[0] as $keyy => $str )
			{
				// 找到 img 标签 跳过
				if( strstr($str,'img')  || strstr($str,'<p><br/>') )
				{
					continue;
				}else
				{
					$patt = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
					preg_match_all($patt,$str,$array);
					// 简短描述
					$content = $array[0][0];
					// 简短描述
					// $content =  rtrim( ltrim( $str,"<p>" ),"<br/></p>" );
					break;
				}
			}
			
			if( !isset($content) )
			{
				$content = '';
			}
			
			$wenzhang['content'] = $content;
			$wenzhang['id'] = $article_info -> id;
			$wenzhang['category_name'] = $article_info -> category_name;
			$wenzhang['category_id'] = $article_info -> category_id;
			$wenzhang['title'] = $article_info -> title;
			$wenzhang['create_at'] = $article_info -> create_at;
			$all_wenzhang[$key] = $wenzhang;
		
		}
		
		
		// 粉丝总数
		$fansTotal = DB::table('fans')
				-> where( 'follow_id',$user_id )
				-> count();
		
		// 全部粉丝		
		$fans = DB::table('fans')
			-> select('uid')
			-> where( 'follow_id',$user_id )
			-> get();
		$fans_arr = array();
		foreach( $fans as $key=> $fans_id )
		{
			$fans_arr[$key] = $fans_id -> uid;
		}

		return view('center.index',['user'=>$user,'history'=>$history,'rank'=>$rank,'articles'=>$articles,'all_wenzhang'=>$all_wenzhang,'fansTotal'=>$fansTotal,'fans_arr'=>$fans_arr]);
		
	}
	
	
	// 瀑布流 加载 球吧文章
	public function pubuliu( Request $request )
	{
		// 包含 页数 用户id
		$data = $request -> except('_token');
		$page = intval( $data['page'] ) -1 ;
		$per = 5;
		$start = $per * $page;
		// 查询 该用户的文章
		$articles = DB::table('article as a')
			-> select("a.*",'c.name as category_name')
			-> leftJoin('category as c','c.id','=','a.category_id')
			-> where('a.user_id',$data['user_id'])
			-> whereIn( 'a.status',array(3,4,5) )
			-> orderBy('a.create_at','desc')
			// 从 哪开始
			-> offset($start)
			// 只取10条
			-> limit(5)
			-> get();
		
		if( count($articles) < 1 )
		{
			$this -> show_msg('已全部加载');
		}
		$all_wenzhang = array();
		foreach( $articles as $key=> $article_info )
		{
			$wenzhang = array();
			// 文章简短介绍
			$pattern = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
			preg_match_all($pattern,$article_info->content,$arr);
			foreach( $arr[0] as $keyy => $str )
			{
				// 找到 img 标签 跳过
				if( strstr($str,'img')  || strstr($str,'<p><br/>') )
				{
					continue;
				}else
				{
					// 简短描述
					// $content =  rtrim( ltrim( $str,"<p>" ),"<br/></p>" );
					$patt = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
					preg_match_all($patt,$str,$array);
					// 简短描述
					$content = $array[0][0];
					break;
				}
			}
			
			if( !isset($content) )
			{
				$content = '';
			}
			$wenzhang['content'] = $content;
			$wenzhang['id'] = $article_info -> id;
			$wenzhang['user_id'] = $article_info -> user_id;
			$wenzhang['category_name'] = $article_info -> category_name;
			$wenzhang['category_id'] = $article_info -> category_id;
			$wenzhang['title'] = $article_info -> title;
			$wenzhang['create_at'] = $article_info -> create_at;
			$all_wenzhang[$key] = $wenzhang;
		
		}
		
		// 传递 数组 json 格式
		exit( json_encode($all_wenzhang) );
		
	}
	
	
	
	
	
	
}
